package cn.xshi.sys.web;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import cn.xshi.sys.form.SysPathForm;
import cn.xshi.sys.model.SysPath;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysPathService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @Desc 文件路径
 * @Author 邓纯杰
 * @CreateTime 2024-04-24 10:00:02
 */
@RestController
@RequestMapping("/sysPath")
@Api(value = "文件路径API",tags = "文件路径API",description = "文件路径API")
public class SysPathController extends BaseAction {

	@Resource
	private SysPathService sysPathService;

	/**
	 * 查询文件路径列表并分页
	 * @param sysPathForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询文件路径列表并分页", notes="查询文件路径列表并分页")
	@GetMapping(value="/list")
	@Auth(value = "/sysPath/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<SysPath>> getSysPathListByCondition(SysPathForm sysPathForm,Integer pageNo, Integer pageSize){
		commonHPagerByPageNo(pageNo,pageSize);
		List<SysPath> sysPathList = sysPathService.getSysPathListByCondition(sysPathForm);
		PageInfo<SysPath> page = new PageInfo<SysPath>(sysPathList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	 * 查询单个文件路径
	 * @param id
	 */
	@ApiOperation(value="查询单个文件路径", notes="查询单个文件路径")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysPath/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<SysPath> getSysPathById(@PathVariable("id")String id){
		SysPath sysPath = sysPathService.getSysPathById(id);
		if(!StringUtil.isEmpty(sysPath.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysPath.getCreateId());
			if(null != createBy){
				sysPath.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysPath.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysPath.getUpdateId());
			if(null != modifiedBy){
				sysPath.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysPath);
	}

	/**
	 * 全部
	 */
	@ApiOperation(value="查询全部文件路径", notes="查询全部文件路径")
	@GetMapping(value="/lists")
	@Auth(value = "/sysPath/lists",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult<List<SysPath>> lists(){
		Map<String,Object> condition = new HashMap<>();
		List<SysPath> sysPathList = sysPathService.getSysPathListAllByCondition(condition);
		return outDataStr(sysPathList);
	}

	/**
	 * 添加
	 * @param sysPath
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个文件路径", notes="创建单个文件路径")
	public BaseResult addSysPath(@RequestBody SysPath sysPath){
		int i = 0;
		if(null != sysPath){
			sysPath.setCreateTime(getDate());
			sysPath.setId(toUUID());
			i= sysPathService.addSysPath(sysPath);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 修改
	 * @param sysPath
	 */
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个文件路径", notes="编辑单个文件路径")
	public BaseResult updateSysPath(@RequestBody SysPath sysPath){
		int i = 0;
		if(null != sysPath){
			sysPath.setUpdateId(getCurrentAccountId());
			sysPath.setUpdateTime(getDate());
			i= sysPathService.updateSysPath(sysPath);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除文件路径", notes="删除文件路径")
	public BaseResult delSysPath(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysPathService.delSysPath(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 导出
	 * @param response
	 * @param id
	 */
	@ApiOperation(value="导出", notes="导出")
	@GetMapping(value = "/export")
	public void exportPath(HttpServletResponse response, String id) {
		sysPathService.exportPath(response,id);
	}

	/**
	 * 导入
	 * @param request
	 * @return
	 */
	@ApiOperation(value="导入", notes="导入")
	@PostMapping(value = "/import")
	public BaseResult importConstant(HttpServletRequest request, HttpServletResponse response) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;//转型为MultipartHttpRequest：
		MultiValueMap<String, MultipartFile> multipartFileMultiValueMap =  multipartRequest.getMultiFileMap();//获取所有文件
		Iterator<Map.Entry<String, List<MultipartFile>>> iterator = multipartFileMultiValueMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<String, List<MultipartFile>> entry = iterator.next();
			for (MultipartFile multipartFile : entry.getValue()) {
				sysPathService.importPath(request,response,multipartFile);
			}
		}
		return BaseResult.success();
	}
}
